import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import update from 'react-addons-update';
import nattyStorage from 'natty-storage';
import DB from '../../../app/db';
import {Control} from 'react-keeper';

export default Reflux.createStore({
    listenables: [Actions],
    data: {
      ...PageConst,
      suggestList:[],
      domain:"",
      mediaUploadPath:"",
      pageNumber:0, //当前页,从0开始
      totalPage:1,  //总共的页数
      pageSize:8  //每页多少条
    },

    onFetch: function (params, cb) {
        let t = this;
        t.updateComponent();
        cb && cb(t.data);
    },

    updateComponent: function () {
        this.trigger(this.data);
    },

    getInitialState: function () {
        return this.data;
    },

    onLoadSuggestionList:function(suggesttype){
      var that=this;
      DB.WechatAPI.loadSuggestionList({page:this.data.pageNumber,size:this.data.pageSize,suggesttype:suggesttype}).then(
        function (content) {
          that.data.suggestList=update(that.data.suggestList, {$push: content.dgzsList});
          that.data.domain=update(that.data.domain, {$set: content.domain});
          that.data.mediaUploadPath=update(that.data.mediaUploadPath, {$set: content.mediaUploadPath});
          that.data.pageSize=update(that.data.pageSize, {$set: content.size});
          that.data.totalPage=update(that.data.totalPage, {$set: content.totalPage});
          that.data.pageNumber=update(that.data.pageNumber, {$set: content.currentPage});
          that.updateComponent();
        },
        function (error) {
          console.log(error);
        }
      );
    },

    onClearList:function(){
      this.data.suggestList=update(this.data.suggestList, {$set: []});
      this.data.pageNumber=update(this.data.pageNumber, {$set: 0});
      this.updateComponent();
    },

    onScrollLoadMore:function(){
      if(this.data.pageNumber+1<this.data.totalPage){
        this.data.pageNumber=update(this.data.pageNumber, {$set: this.data.pageNumber+1});
        this.onLoadSuggestionList();
      }
    },

    updateComponent: function () {
      this.trigger(this.data);
    },

    getInitialState: function () {
      return this.data;
    }
});
